<template>
  <div class="reset-password-container">
    <el-card class="reset-password-card">
      <template #header>
        <div class="card-header">
          <h2>重置密码</h2>
        </div>
      </template>
      
      <el-form 
        ref="resetFormRef"
        :model="resetForm"
        :rules="rules"
        label-width="0"
        size="large"
      >
        <el-form-item prop="password">
          <el-input
            v-model="resetForm.password"
            type="password"
            placeholder="新密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="resetForm.confirmPassword"
            type="password"
            placeholder="确认新密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button 
            type="primary" 
            class="submit-button"
            :loading="loading"
            @click="handleSubmit"
          >
            重置密码
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const route = useRoute()
const resetFormRef = ref(null)
const loading = ref(false)

// 表单数据
const resetForm = reactive({
  password: '',
  confirmPassword: '',
  token: route.params.token
})

// 验证密码是否一致
const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== resetForm.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

// 表单验证规则
const rules = {
  password: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass, trigger: 'blur' }
  ]
}

// 提交处理
const handleSubmit = async () => {
  if (!resetFormRef.value) return
  
  await resetFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        const { confirmPassword, ...resetData } = resetForm
        
        await axios.post('/api/auth/reset-password', resetData)
        
        ElMessage.success('密码重置成功！')
        router.push('/login')
      } catch (error) {
        const message = error.response?.data?.message || '重置失败'
        ElMessage.error(message)
      } finally {
        loading.value = false
      }
    }
  })
}

// 验证重置token
onMounted(async () => {
  try {
    await axios.get(`/api/auth/verify-reset-token/${resetForm.token}`)
  } catch (error) {
    ElMessage.error('重置链接无效或已过期')
    router.push('/forgot-password')
  }
})
</script>

<style scoped>
.reset-password-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.reset-password-card {
  width: 400px;
}

.card-header {
  text-align: center;
}

.submit-button {
  width: 100%;
}
</style> 